<?php
//覆盖
//df_writefile_cover
function _fwf($filename,$conntent)
{  
	$file=fopen($filename,"w+b");
	$ret =fwrite($file,$conntent);
	fclose($file);
	return  $ret;
}
//-2EBD//
//-1EBD//
//EBD//
/********************************************/
/********************************************/
/*fileName: 
 *internal version: 0001
 *version : 1.0.0 [programming] [official version]
 *author : HeShengyu
 *Date : 2014.08.27 
 *email: koolfer@163.com
 *end date:2014.08.27
 *http://www.cnblogs.com/ssnowy/
*/
class Cldir{
	public static function writeFileW1b($filename,$conntent){ 
	    $file=fopen($filename,"w+b");
		if(is_array($conntent)){
            foreach($conntent as $v){
				//将数组写入文件，每次追加一个数组的内容　
			    $ret =fwrite($file,$v);	
			}	
		}else{
		     $ret =fwrite($file,$conntent);
		}
	    fclose($file);
	    return  $ret;
    }	
	//Cldir::writeFile(
    //array('/a1koolfer/b/d/e/f/g/h/o',__FILE__)
    //,'abc.txt','文件内容'.date('Y-m-d H:i:s',time())
    //);
	public static function writeFile($path,$filename,$conntent='conntent'){
		if(count($path)==1){
	        $thePath=self::mkdir($path[0]);
		}
		if(count($path)==2){
			$thePath=self::mkdir($path[0],$path[1]);
		}
		//echo '{'.$thePath.'}';	
		$result=self::writeFileW1b($thePath.$filename,$conntent);
		echo '生成文件:'.$thePath.$filename.'</br>'; 
		echo $result?'ok</br>':'error</br>';
	}
	public static function mkdir($path,$__file__=''){
	    if(!empty($__file__)){
		    return self::mkdirNotTarget($path,$__file__);	
		}
		return self::mkdirs($path);	
	}
	public static function mkdirs($path){
    $target=preg_replace('/([A-Z]:[\/\\\\]){0,1}[\d\D]{0,}/','\\1',$path);
	if($target==''){
	    trigger_error("Cldir::mkdir,目标磁盘未定义",E_USER_ERROR);	
	}
    preg_match_all("/\/{1,}([^\/]{1,})/",$path,$matches);
	$stauts= $target;
		foreach($matches[1] as $v){
			$stauts=$stauts.($v."/");
			if(!file_exists($stauts) or !is_dir($stauts)){
				echo($stauts."<br>");
				echo mkdir($stauts,0777)
				?"<font color='#FF6600''>create dir ok</font><br>"
				:"<font color='#FF6600''>create dir error</font><br>";
			}else{
				echo $stauts.'目录已经存在</br>';	
			}
		}
	return $stauts;
    }	
	public static function mkdirNotTarget($path,$__file__){
		if(empty($path) or empty($__file__)){
		    trigger_error("Cldir::mkdirNotTarget,参数1,2都不能为空,
			参数1例如:/a/b参数2可以为__FILE__",E_USER_ERROR);	
			exit;	
		}
	     preg_match_all("/\/{1,}([^\/]{1,})/",$path,$matches);	
		 $stauts= self::getCurrentDir($__file__);
			foreach($matches[1] as $v){
				$stauts=$stauts.($v."/");
				if(!file_exists($stauts) or !is_dir($stauts)){
					echo($stauts."<br>");
					echo mkdir($stauts,0777)
					?"<font color='#FF6600''>create dir ok</font><br>"
					:"<font color='#FF6600''>create dir error</font><br>";
				}else{
					echo $stauts.'目录已经存在</br>';	
				}
			}
		return $stauts;	
	}
	public static function getCurrentDir($__file__){
		return preg_replace( array('/\/{1,}/'), array('/'),
	    str_replace( '\\','/' , dirname($__file__).'/' )   
		       );
		 	
	}
}
/********************************************/
class clerror{
    public static $cltab;
	public function __construct(){
	    self::$cltab=new Cltable();	
		$this->setErrorHandler();
	}
	public function __destruct(){}	
	public function setErrorHandler(){
	    set_error_handler ('clerror::errorHandler');
    }
	public static function errorHandler (
	$error_level, $error_message, $file, $line,$error_context) {
		//global $tab;
    switch ($error_level) {
		//提醒级别
        case E_NOTICE:
		 $error_type = 'E_NOTICE';
            break;
        case E_USER_NOTICE:
            $error_type = 'E_USER_NOTICE';
            break;
		
		//警告级别
        case E_WARNING:
		$error_type = 'E_WARNING';
            break;
        case E_USER_WARNING:
            $error_type = 'E_USER_WARNING';
            break;
		
		//错误级别
        case E_ERROR:
		    $error_type = 'E_ERROR';
            break;
        case E_USER_ERROR:
            $error_type = 'E_USER_ERROR';
            $EXIT = TRUE;
            break;
		
		//其他未知错误
        default:
            $error_type = '['.$error_level.']';
            $EXIT = TRUE;
            break;
    }
 
	if(array_key_exists("php_errormsg", $error_context)){
		$php_errormsg=$error_context['php_errormsg'];
	}else{
	    $php_errormsg='';	
	}
	
	self::$cltab->export (array(
	'level'=>$error_level,
	'type'=>$error_type,
	'message'=>$error_message,
	'file'=>$file, 
	'line'=>$line,
	'error_context'=> $php_errormsg
	 ));
     //return true 隐藏系统的报错;
        return true;
     }
}
/********************************************/
class clfile{
	//覆盖
    //writefile_cover
	public static function writeFileW1b($filename,$conntent){ 
	    $file=fopen($filename,"w+b");
	    $ret =fwrite($file,$conntent);
	    fclose($file);
	    return  $ret;
    }
	
	public static function loadFiles($aFileNames,$match='',&$arr=''){
	$aRet=array();
	$arr =array();
    foreach($aFileNames as $v){
		if($match=='' || preg_match($match,$v)){
			$arr[]=$v;
		$aRet[]=
							/*implode( "",file($v) 为一字符串　file($v为数组，　*/ 
							/*implode将字串变成数组*/
						  
						  implode( "",  array_slice( file($v) , 1 , -1 )  ) 
						   ."/********************************************/\n"   ;
		}
	}
	     
	return $aRet;
	
    }	
	
	public static function getFileNames($aFileNames,$match='',&$arr=''){
 
	$arr =array();
    foreach($aFileNames as $v){
		if($match=='' || preg_match($match,$v)){
			$arr[]=$v;
		}
	}
	     
	return $arr;
	
    }	
	
}

/********************************************/
class Clstr{
    public static function sdefault($str,$defaultValue){
	    if(strlen($str)==0){
		    return $defaultValue; 
		}
		return $str;	
	}	
}
/********************************************/

class sys{
	public static function _print_r($v,$left=-1,$ret="My"){
		$left+=1;
		if (is_array($v) ){
			return self::arrayToString($v,$left,$ret);
		} 
		return   $ret.$v."\n";	 
	}

	public static function arrayToString($v,$left,$ret){	
			$ret.="Array\n".str_repeat('  ',$left*4)."(\n";
			foreach ($v as $key=>$val)
			{
				$parmret=$ret.str_repeat('  ',$left*4+2)."[$key] => ";
				$ret=self::recursionPrintR($val,$left,$parmret)	;			 
			}
			 $ret.=str_repeat('  ',$left*4).")\n\n";	
		 return $ret;
	}
 
//递归调用_print_r
	public static function recursionPrintR($val,$left,$ret){	
		if (is_array($val)){
			return self::_print_r($val,$left,$ret);
		}
			return $ret."$val\n";	 		 	
	}
	public static function print_r($v,$left=-1,$ret="My"){
	    echo self::_print_r($v,$left,$ret);
	}
}
/********************************************/
class Cltable{	
    public static $emptyTable;
	public $carray;
	public $tableContent;
	const Width="65px";
    public function __construct($arr='empty',$sWidth=''){
		self::setEmptyTable();
		$this->tableContent=array();
		$this->checkArray($arr);
		$arrcontent=$this->carray;
		$this->mkContent($arrcontent,$sWidth);
	}
	public static function setBg($num){
		return  ($num%2)==0 ? "#f0f0f0;" : "#fcfcfc;";
	}
    public function export($arr='empty',$sWidth=''){
		if($arr=='empty'){
			echo Cltable::$emptyTable;	
			foreach( $this->tableContent as $k=>$v){
				echo $v;	
			}
			 echo '</table></div>';
		}else{
			$this->exportArr($arr,$sWidth);
		}
	}
	public function exportArr($arr,$sWidth=''){
	    $this->mkContent($arr,$sWidth);	
		$this->export();
	}
	public function mkContent($arr,$sWidth){	
	    $i=0;
		$this->tableContent=array();
		foreach( $arr as $k=>$v){
		
			$this->tableContent[]=
			 "<tr><td  class='num' "
			 ." style='display:block;color:#999;font-size:18px;"
			 ."width:".$sWidth.";overflow:hidden;'><b>".$k
             ."</b></td><td style='border-color:#f50;width:4000px;"
			 ." border-left-style:solid; border-left-width:4px;"
			 ."height:12px; padding-left:2px;background-color:"
			 .self::setBg($i)."'>".$v."</td></tr>";
			 $i++;
		}
		
    }
	public function __destruct(){}	
	public function checkArray($arr){
		!is_array($arr) ? $arr=array('Is not array!') : true;
		$this->carray=$arr;
	}
	public static function setEmptyTable(){
	   Cltable::$emptyTable =
	   '<div style="border:10px #f50 solid;"><table  border="0" cellspacing="0" cellpadding="0" style="background-color:#fff; " >';	
	}
	
}
/********************************************/

/********************************************/
function test(){
    echo time(); 
}
/********************************************/

?>